package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.common.BizException;
import com.yuelao.yuelao_backend.common.ErrorCode;
import com.yuelao.yuelao_backend.dto.TaskDTO;
import com.yuelao.yuelao_backend.dto.request.CompleteTaskRequest;
import com.yuelao.yuelao_backend.service.TaskService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tasks")
@Slf4j
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 获取每日任务列表
     * GET /tasks/daily
     * @return 每日任务列表
     */
    @GetMapping("/daily")
    public ApiResponse<List<TaskDTO>> getDailyTasks() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        List<TaskDTO> dailyTasks = taskService.getDailyTasks(currentUserId);
        return ApiResponse.ok(dailyTasks);
    }

    /**
     * 获取签到信息
     * GET /tasks/checkin
     * @return 签到信息
     */
    @GetMapping("/checkin")
    public ApiResponse<Map<String, Object>> getCheckinInfo() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        Map<String, Object> checkinInfo = taskService.getCheckinInfo(currentUserId);
        return ApiResponse.ok(checkinInfo);
    }

    /**
     * 执行签到
     * POST /tasks/checkin/do
     * @return 签到结果
     */
    @PostMapping("/checkin/do")
    public ApiResponse<Map<String, Object>> doCheckin() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        Map<String, Object> result = taskService.doCheckin(currentUserId);
        return ApiResponse.ok(result);
    }

    /**
     * 完成任务
     * POST /tasks/{taskId}/complete
     * @param taskId 任务ID
     * @return 是否成功
     */
    @PostMapping("/{taskId}/complete")
    public ApiResponse<Boolean> completeTask(@PathVariable Long taskId) {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        if (taskId == null || taskId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "任务ID无效");
        }
        boolean result = taskService.completeTask(currentUserId, taskId);
        return ApiResponse.ok(result);
    }

    /**
     * 获取成就列表
     * GET /tasks/achievements
     * @return 成就列表
     */
    @GetMapping("/achievements")
    public ApiResponse<List<TaskDTO>> getAchievements() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        List<TaskDTO> achievements = taskService.getAchievements(currentUserId);
        return ApiResponse.ok(achievements);
    }

    /**
     * 观看广告获得奖励
     * POST /tasks/watch-ad
     * @return 是否成功
     */
    @PostMapping("/watch-ad")
    public ApiResponse<Boolean> watchAdvertisement() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        boolean result = taskService.watchAdvertisement(currentUserId);
        return ApiResponse.ok(result);
    }

    /**
     * 获取广告信息
     * GET /tasks/ad-info
     * @return 广告信息
     */
    @GetMapping("/ad-info")
    public ApiResponse<Map<String, Object>> getAdInfo() {
        Map<String, Object> adInfo = taskService.getAdInfo();
        return ApiResponse.ok(adInfo);
    }

    /**
     * 获取VIP专属任务列表
     * GET /tasks/vip
     * @return VIP任务列表
     */
    @GetMapping("/vip")
    public ApiResponse<List<TaskDTO>> getVipTasks() {
        Long currentUserId = UserContextUtil.getCurrentUserId();
        List<TaskDTO> vipTasks = taskService.getVipTasks(currentUserId);
        return ApiResponse.ok(vipTasks);
    }
}